home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-06-09 | 5.3 KB | 192 lines | [TEXT/CWIE] |
- // Copyright (C) 1999 Eric Roccasecca. All rights reserved.
-
- #include <TextServices.h>
- #include <AppleEvents.h>
-
- #include "common.h"
- #include "X_Ray.h"
-
-
- enum {
- kNewWindowProcInfo = kPascalStackBased
- | RESULT_SIZE (SIZE_CODE(sizeof(WindowPtr)))
- | STACK_ROUTINE_PARAMETER(1,SIZE_CODE(sizeof(void*)))
- | STACK_ROUTINE_PARAMETER(2,SIZE_CODE(sizeof(Rect*)))
- | STACK_ROUTINE_PARAMETER(3,SIZE_CODE(sizeof(char*)))
- | STACK_ROUTINE_PARAMETER(4,SIZE_CODE(sizeof(Boolean)))
- | STACK_ROUTINE_PARAMETER(5,SIZE_CODE(sizeof(short)))
- | STACK_ROUTINE_PARAMETER(6,SIZE_CODE(sizeof(WindowPtr)))
- | STACK_ROUTINE_PARAMETER(7,SIZE_CODE(sizeof(Boolean)))
- | STACK_ROUTINE_PARAMETER(8,SIZE_CODE(sizeof(long)))
- };
-
- /* Globals */
- Rect gWinRect;
- WindowPtr gWin;
- WindowRecord gWinRec;
- Boolean gDone = false;
- OSErr gErr;
- AEEventHandlerUPP gQuitHandlerUPP;
- PicHandle gWinPic;
- flt_GlobalCommPtr gCommRec = nil;
-
- UniversalProcPtr gOldNewWindowPtr, gNewWindowUPP;
- Boolean gMakingFloaters = false;
-
- /* Prototypes */
- void HandleEvent (EventRecord *theEvent);
- pascal OSErr MyQuitHandler (const AppleEvent *theAppleEvent, const AppleEvent * reply, long handlerRefcon);
- Boolean PrepForMakingWindows (void);
- pascal WindowPtr flt_NewWindow (void *wStorage, const Rect *boundsRect, ConstStr255Param title, Boolean visible, short theProc, WindowPtr behind, Boolean goAwayFlag, long refCon);
- void AppCodeFragmentDied (void);
-
-
- void main (void)
- {
- EventRecord theEvent;
-
- InitGraf (&qd.thePort);
-
- gWinPic = GetPicture (128);
- if (gWinPic == nil)
- ExitToShell();
-
- PrepForMakingWindows();
-
- // use the picture's rectangle for the window size
- gWinRect = (*gWinPic)->picFrame;
- SetRect (&gWinRect, 0, 0, gWinRect.right - gWinRect.left, gWinRect.bottom - gWinRect.top);
- OffsetRect (&gWinRect, 50, 50);
-
- gMakingFloaters = true;
- gErr = NewTransparentServiceWindow (&gWinRec, &gWinRect, "\p", true, kWindowFloatProc, (WindowPtr) -1, true, (ComponentInstance)kSystemProcess, &gWin, nil, nil);
- gMakingFloaters = false;
- if (gErr != noErr)
- ExitToShell();
-
- HiliteWindow (gWin, true);
- SetPort (gWin);
- EraseRect (&gWin->portRect);
- DrawPicture (gWinPic, &gWin->portRect);
-
- gErr = Gestalt (kfltGestalt, (long*)&gCommRec);
- if (gErr == noErr)
- {
- gErr = GetCurrentProcess (&gCommRec->daemonPSN);
- gCommRec->daemonWindow = gWin;
- gCommRec->daemonZone = GetZone();
- gCommRec->daemonQDGlobals = &qd;
- gCommRec->daemonCloseWindow = false;
- }
- else
- ExitToShell();
-
- gQuitHandlerUPP = NewAEEventHandlerProc (MyQuitHandler);
- gErr = AEInstallEventHandler (kCoreEventClass, kAEQuitApplication, gQuitHandlerUPP, false, false);
-
- while (!gDone)
- {
- WaitNextEvent (everyEvent, &theEvent, 60, nil);
- HandleEvent (&theEvent);
- }
-
- if (gWin)
- {
- gErr = CloseTransparentServiceWindow (gWin);
- gWin = nil;
- }
-
- if (gCommRec)
- {
- gCommRec->daemonPSN.lowLongOfPSN = 0;
- gCommRec->daemonPSN.highLongOfPSN = 0;
- gCommRec->daemonWindow = nil;
- gCommRec->daemonZone = 0;
- gCommRec->daemonQDGlobals = nil;
-
- gCommRec = nil;
- }
- }
-
-
- void HandleEvent (EventRecord *theEvent)
- {
- if (theEvent->what == nullEvent)
- {
- if (gCommRec)
- {
- // see if we were signaled to quit
- if (gCommRec->daemonCloseWindow)
- {
- gDone = true;
- gCommRec->daemonCloseWindow = false; // reset this
- }
- }
-
- // a handy way to service update events in text service floating windows
- if (!EmptyRgn (((WindowPeek)gWin)->updateRgn))
- {
- DrawPicture (gWinPic, &gWin->portRect);
- SetEmptyRgn (((WindowPeek)gWin)->updateRgn);
- }
- }
- else if (theEvent->what == kHighLevelEvent)
- gErr = AEProcessAppleEvent (theEvent);
- }
-
-
- pascal OSErr MyQuitHandler (const AppleEvent* theAppleEvent, const AppleEvent* reply, long handlerRefcon)
- {
- #pragma unused (theAppleEvent, reply, handlerRefcon)
- gDone = true;
- return noErr;
- }
-
-
- // applies a patch to NewWindow so TSM windows can be colorized
- // this is useful for pre Mac OS 8.5 systems
- // 8.5 or higher there is finally a system call for color text service windows
- Boolean PrepForMakingWindows (void)
- {
- // Apply local patch to NewWindow
- gOldNewWindowPtr = NGetTrapAddress (_NewWindow, ToolTrap);
- gNewWindowUPP = NewRoutineDescriptor ((ProcPtr)flt_NewWindow, kNewWindowProcInfo, GetCurrentArchitecture());
- if (!gNewWindowUPP)
- return false;
- NSetTrapAddress (gNewWindowUPP, _NewWindow, ToolTrap);
- return true;
- }
-
-
- // fakes out text services maanger and returns a color window instead of black and white window
- pascal WindowPtr flt_NewWindow (void *wStorage, const Rect *boundsRect, ConstStr255Param title, Boolean visible, short theProc, WindowPtr behind, Boolean goAwayFlag, long refCon)
- {
- if (!gMakingFloaters)
- return (WindowPtr)CallUniversalProc (gOldNewWindowPtr, kNewWindowProcInfo, wStorage, boundsRect, title, visible, theProc, behind, goAwayFlag, refCon);
- else
- return NewCWindow (wStorage, boundsRect, title, visible, theProc, behind, goAwayFlag, refCon);
- }
-
-
- // used when the code fragment that contains this app goes away
- // to make sure it cleans up after itself
- // this is best used in cases of crashes or being unable to fully intialize
- void AppCodeFragmentDied (void)
- {
- if (gWin)
- {
- gErr = CloseTransparentServiceWindow (gWin);
- gWin = nil;
- }
-
- if (gCommRec)
- {
- gCommRec->daemonPSN.lowLongOfPSN = 0;
- gCommRec->daemonPSN.highLongOfPSN = 0;
- gCommRec->daemonWindow = nil;
- gCommRec->daemonZone = 0;
- gCommRec->daemonQDGlobals = nil;
-
- gCommRec = nil;
- }
- }